package com.play.ballen.synthetic.vm

import androidx.lifecycle.MutableLiveData
import com.play.ballen.R
import com.play.ballen.home.entity.HomeBallInfo
import com.play.ballen.mall.entity.RateInfo
import com.play.ballen.network.ApiRepository
import com.play.ballen.network.BallenRxHelper
import com.play.ballen.pack.entity.PackBallInfo
import com.play.ballen.synthetic.entity.Guess
import com.play.ballen.synthetic.entity.GuessInfo
import com.play.ballen.synthetic.entity.Notice
import com.play.ballen.synthetic.entity.SyntheticBallInfo
import com.play.ballen.synthetic.entity.SyntheticGuessInfo
import com.play.ballen.synthetic.entity.SyntheticInfo
import com.yuven.appframework.busevent.SingleLiveEvent
import com.yuven.appframework.network.RxSchedulerHelper
import com.yuven.appframework.util.addToDisposable
import com.yuven.baselib.component.viewmodel.BaseViewModel
import com.yuven.baselib.utils.Logger
import com.yuven.baselib.utils.ResourceUtil

open class SyntheticVM : BaseViewModel() {

    protected val model = ApiRepository()

    //首页-合成fragment
    val listBall = MutableLiveData<List<SyntheticBallInfo>>()

    val listGuess = MutableLiveData<GuessInfo>()

    val actionGetBallFailure = SingleLiveEvent<String>()

    //合成页面基础信息
    val syntheticInfo = SingleLiveEvent<SyntheticInfo>()

    //提交合成成功
    val actionSyntheticSuccess = SingleLiveEvent<Void>()

    //合成-弹窗选择篮球
    val listSyntheticChooseBall = MutableLiveData<List<PackBallInfo>?>()

    //投票
    val syntheticGuessInfo = SingleLiveEvent<SyntheticGuessInfo>()

    val actionGuessSuccess = SingleLiveEvent<String>()

    val rateInfo = SingleLiveEvent<RateInfo>()

    val notice = MutableLiveData<Notice>()

    val guessLog = MutableLiveData<List<Guess>>()

    var page = 1
    private val pageSize = 20
    fun getNotice() {
        model.getNotice()
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .subscribe({
                notice.value = it
            }, { throwable ->
//                actionGetBallFailure.value = throwable.message
            }).addToDisposable(compositeDisposable)

    }

    fun getGuessLog() {
        val map = mutableMapOf<String, Any>()
        map["page"] = page
        map["pagesize"] = pageSize
        model.getGuessLog(map)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .subscribe({
                guessLog.value = it
            }, { throwable ->
                actionGetBallFailure.value = throwable.message
            }).addToDisposable(compositeDisposable)

    }



    fun getGuessInfoList() {
        model.getGuessInfoList()
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .subscribe({
                Logger.i("SyntheticVM.getSyntheticList success")
                listGuess.value = it
            }, { throwable ->
                actionGetBallFailure.value = throwable.message
                Logger.i("SyntheticVM.getSyntheticList error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)

    }


    fun getSyntheticList() {
        model.getSyntheticList()
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .subscribe({
                Logger.i("SyntheticVM.getSyntheticList success")
                listBall.value = it
            }, { throwable ->
                actionGetBallFailure.value = throwable.message
                Logger.i("SyntheticVM.getSyntheticList error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }


    fun getSyntheticInfo(id: String) {
        showDialog.setValue(true, ResourceUtil.getString(R.string.loading))
        model.getSyntheticInfo(id)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                showDialog.setValue(false)
            }
            .subscribe({
                Logger.i("SyntheticVM.getSyntheticInfo success")
                syntheticInfo.value = it
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("SyntheticVM.getSyntheticInfo error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }

    fun getSyntheticGuessInfo(id: String) {
        showDialog.setValue(true, ResourceUtil.getString(R.string.loading))
        model.getSyntheticGuessInfo(id)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                showDialog.setValue(false)
            }
            .subscribe({
                Logger.i("SyntheticVM.getSyntheticGuessInfo success")
                syntheticGuessInfo.value = it
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("SyntheticVM.getSyntheticGuessInfo error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }


    fun memberBasketBall() {
        showDialog.setValue(true, ResourceUtil.getString(R.string.loading))
        val map = mutableMapOf<String, Any>()
        map["page"] = 1
        map["pagesize"] = 10000
        map["status"] = 1
        model.memberBasketBall(map)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                showDialog.setValue(false)
            }
            .subscribe({ data ->
                listSyntheticChooseBall.value = data.list
                Logger.i("SyntheticVM.memberBasketBall success list = $data")
            }, { throwable ->
                actionGetBallFailure.value = throwable.message
                Logger.i("SyntheticVM.memberBasketBall error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }

    fun syntheticSubmit(id: String, ids: String) {
        showDialog.setValue(true, ResourceUtil.getString(R.string.loading))
        val map = mutableMapOf<String, Any>()
        map["id"] = id
        map["ids"] = ids
        model.syntheticSubmit(map)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                showDialog.setValue(false)
            }
            .subscribe({
                Logger.i("SyntheticVM.syntheticSubmit success")
                actionSyntheticSuccess.call()
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("SyntheticVM.syntheticSubmit error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }

    fun syntheticGuess(params: MutableMap<String, Any>) {
        showDialog.setValue(true, ResourceUtil.getString(R.string.loading))
        model.syntheticGuess(params)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                showDialog.setValue(false)
            }
            .subscribe({
                Logger.i("SyntheticVM.syntheticGuess success")
                actionGuessSuccess.call()
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("SyntheticVM.syntheticGuess error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)

    }


    fun basketballBuyRate() {
        showDialog.setValue(true, ResourceUtil.getString(R.string.loading))
        model.basketballBuyRate()
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                showDialog.setValue(false)
            }
            .subscribe({
                Logger.i("MallVM.basketballBuyRate success")
                rateInfo.value = it
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("MallVM.basketballBuyRate error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }
}